﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.CallRoute;


public partial class OrderManagement_Order_Create : PageBase
{
    #region 全局变量
    private decimal customerID = 0;
    private decimal userID = 0;
    private decimal chainID = 0;
    private const string CUSTOMER_ID = "customerID";
    private const string USER_ID = "userID";
    private const string CHAIN_ID = "chainID";
    private CUSTOMERS customer;
    private CustomerManager customerManager = new CustomerManager();
    private ChainManager chainManager = new ChainManager();
    private SupplyManager supplyManager = new SupplyManager();
    private CustomerRouteManager routeManager = new CustomerRouteManager();
    private Jumpton.SFA.Server.BusinessRule.MasterData.ProductManager productManager = new Jumpton.SFA.Server.BusinessRule.MasterData.ProductManager();
    private OrderHeaderManager headManager = new OrderHeaderManager();
    private OrderLineManager lineManager = new OrderLineManager();
    private ORDER_HEADERS orderHead;
    private ORDER_LINES orderLines;
    private DataTable dtProducts;
    private DataTable dtFreeGoods;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request[CUSTOMER_ID]))
            decimal.TryParse(Request[CUSTOMER_ID], out customerID);

        if (!string.IsNullOrEmpty(Request[USER_ID]))
            decimal.TryParse(Request[USER_ID], out userID);

        if (!string.IsNullOrEmpty(Request[CHAIN_ID]))
            decimal.TryParse(Request[CHAIN_ID], out chainID);

        btnConfirm.OnClientClick = btn.OnClientClick = "Submit(this);";

        if (!IsPostBack)
        {
            this.InitForm();
            btnAddProduct.Attributes.Add("onclick", "OpenWindow('" + txtHidProduct.ClientID + "','"+MyStateManager.UserID+"','"+customerID+"');return false;");
        }

        rv1.MaximumValue = DateTime.Now.ToShortDateString();
        rv1.MinimumValue = DateTime.Now.Year +"-"+ DateTime.Now.Month + "-1";
        rv1.Type = ValidationDataType.Date;

    }


    #region 页面初始化
    /// <summary>
    /// 页面初始化
    /// </summary>
    private void InitForm()
    {
        #region 检查客户ID有效性

        //检查客户ID有效
        customer = customerManager.Load(customerID);
        if (customerID == 0 || customer == null)
        {
            Response.Clear();
            Response.Write("选择的客户无效!");
            Response.End();
            return;
        }

        #endregion

        //隐藏订单确认界面
        pnlConfirm.Visible = false;

        //设置订单时期为当日
        txtOrderDate.Text = DateTime.Now.ToShortDateString();
        lblCustomer.Text = customer.NAME;

        #region 绑定经销商
       
        //根据门店绑定经销商
        ddlChains.DataSource = chainManager.GetDistribution(Decimal.Parse(customerID.ToString()));
        ddlChains.DataTextField = "CHAINS_NAME";
        ddlChains.DataValueField = "CHAINS_ID";
        ddlChains.DataBind();
        ddlChains.Items.Insert(0, new ListItem("--请选择--", ""));

        //如果参数里有经销商ID默认就选中
        if (chainID != 0)
            ddlChains.SelectedValue = chainID.ToString();
        ddlChains_SelectedIndexChanged("", EventArgs.Empty);

        #endregion

        #region 绑定订单来源

        DictionaryItemsManager dim = new DictionaryItemsManager();
        ddlOrderForm.DataSource = dim.GetItemList(DictionaryEnum.OrderForm, MyStateManager.DomainID);
        ddlOrderForm.DataTextField = "Name";
        ddlOrderForm.DataValueField = "DictionaryItemId";
        ddlOrderForm.DataBind();
        ddlOrderForm.Items.Insert(0, new ListItem("--请选择--", ""));

        #endregion

        #region 经销商用户

        //USER_CHAINS userChains = new UserManager().LoadChainsByUser(MyStateManager.UserID);

        ////经销商用户
        //if (userChains != null)
        //{
        //    //tdOrderForm.InnerHtml = "<input type=\"text\" value=\"经销商自销\" disabled />";
        //    ddlOrderForm.Items.FindByText("经销商自销").Selected = true;
        //    ddlOrderForm.Enabled = false;
        //    txtDeliveryDate.Text = DateTime.Now.ToShortDateString();

        //    //固定经销商
        //    ddlChains.SelectedValue = userChains.CHAINSID.ToString();
        //    ddlChains.Enabled = false;
        //    ddlChains_SelectedIndexChanged("", EventArgs.Empty);

        //    ////绑定业务代表
        //    //ddlUsers.Items.Clear();
        //    //ddlUsers.DataSource = routeManager.GetUsersForOrder(customerID);
        //    //ddlUsers.DataTextField = "NAME";
        //    //ddlUsers.DataValueField = "ID";
        //    //ddlUsers.DataBind();
        //    //ddlUsers.Items.Insert(0, new ListItem("--请选择", ""));
        //    //ddlUsers.SelectedValue = MyStateManager.UserID.ToString();//如果是当前用户，直接选中
        //}

        #endregion

        #region 选择产品的临时表

        dtProducts = new DataTable();
        dtProducts.Columns.Add("ID");
        dtProducts.Columns.Add("CODE");
        dtProducts.Columns.Add("DESCRIPTION");
        dtProducts.Columns.Add("ORDER_PC");
        dtProducts.Columns.Add("ORDER_CASE");
        dtProducts.Columns.Add("RETURN_PC");
        dtProducts.Columns.Add("CHANGE_PC");
        dtProducts.Columns.Add("MANUFACTURE");//制造商
        dtProducts.Columns.Add("CATEGORY");//品类
        dtProducts.Columns.Add("BRAND");//品牌
        dtProducts.Columns.Add("FORM");//系列
        dtProducts.Columns.Add("PIECEPRICE");//单价
        ViewState["Product"] = dtProducts;

        #endregion

        //#region 选择的促销的临时表

        //dtFreeGoods = new DataTable();
        //dtFreeGoods.Columns.Add("ID");
        //dtFreeGoods.Columns.Add("PRODUCT_ID");
        //dtFreeGoods.Columns.Add("PROMOTION_ID");
        //dtFreeGoods.Columns.Add("TO_PRODUCT_ID");
        //dtFreeGoods.Columns.Add("PC");
        //dtFreeGoods.Columns.Add("CASE");
        //ViewState["FreeGood"] = dtFreeGoods;

        //#endregion

    }

    #endregion

    #region 将表格内容更新到临时表
    private void UpdateViewState()
    {
        DataTable dt = new DataTable();
        if (ViewState["Product"] != null)
        {
            dt = ViewState["Product"] as DataTable;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                TextBox txt = gvProductList.Rows[i].FindControl("txtOrderPC") as TextBox;
                dt.Rows[i]["ORDER_PC"] = txt == null ? "" : txt.Text.Trim();

                txt = gvProductList.Rows[i].FindControl("txtOrderCase") as TextBox;
                dt.Rows[i]["ORDER_CASE"] = txt == null ? "" : txt.Text.Trim();

                txt = gvProductList.Rows[i].FindControl("txtReturnPC") as TextBox;
                dt.Rows[i]["RETURN_PC"] = txt == null ? "" : txt.Text.Trim();

                txt = gvProductList.Rows[i].FindControl("txtChangePC") as TextBox;
                dt.Rows[i]["CHANGE_PC"] = txt == null ? "" : txt.Text.Trim();
            }
            ViewState["Product"] = dt;
        }
    }
    #endregion

    #region 下一步
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //更新产品
        this.UpdateViewState();

        dtProducts = ViewState["Product"] as DataTable;
        dtFreeGoods = ViewState["FreeGood"] as DataTable;
        int totalOrderPC = 0;
        int totalOrderCase = 0;
        //int totalFreePC = 0;
        //int totalFreeCase = 0;
        DataRow newRow;


        //创建四个订单表
        DataTable dtOrder = new DataTable();
        dtOrder.Columns.Add("ID");
        dtOrder.Columns.Add("NAME");
        dtOrder.Columns.Add("UOM");
        dtOrder.Columns.Add("QTY");
        dtOrder.Columns.Add("MANUFACTURE");//制造商
        dtOrder.Columns.Add("CATEGORY");//品类
        dtOrder.Columns.Add("BRAND");//品牌
        dtOrder.Columns.Add("FORM");//系列
        dtOrder.Columns.Add("PIECEPRICE");//单价
        //DataTable dtChange = dtOrder.Clone();
        //DataTable dtReturn = dtOrder.Clone();
        //DataTable dtFree = dtOrder.Clone();
        //dtFree.Columns.Add("PROMOTION");
        //dtFree.Columns.Add("OWN_PRODUCT");

        //遍历订单临时表
        foreach (DataRow rowOrder in dtProducts.Rows)
        {

            #region 插入到订单明细

            //订瓶
            if (!string.IsNullOrEmpty(rowOrder["ORDER_PC"].ToString()))
            {
                newRow = dtOrder.NewRow();
                newRow["ID"] = rowOrder["ID"];
                newRow["NAME"] = rowOrder["DESCRIPTION"];
                newRow["UOM"] = "瓶";
                newRow["QTY"] = rowOrder["ORDER_PC"];
                newRow["MANUFACTURE"] = rowOrder["MANUFACTURE"];
                newRow["CATEGORY"] = rowOrder["CATEGORY"];
                newRow["BRAND"] = rowOrder["BRAND"];
                newRow["FORM"] = rowOrder["FORM"];
                newRow["PIECEPRICE"] = rowOrder["PIECEPRICE"];
                dtOrder.Rows.Add(newRow);
                totalOrderPC += int.Parse(rowOrder["ORDER_PC"].ToString());
            }

            //订箱
            if (!string.IsNullOrEmpty(rowOrder["ORDER_CASE"].ToString()))
            {
                newRow = dtOrder.NewRow();
                newRow["ID"] = rowOrder["ID"];
                newRow["NAME"] = rowOrder["DESCRIPTION"];
                newRow["UOM"] = "箱";
                newRow["QTY"] = rowOrder["ORDER_CASE"];
                dtOrder.Rows.Add(newRow);
                totalOrderCase += int.Parse(rowOrder["ORDER_CASE"].ToString());
            }

            #endregion

            //#region 插入到退货单

            //if (!string.IsNullOrEmpty(rowOrder["RETURN_PC"].ToString()))
            //{
            //    newRow = dtReturn.NewRow();
            //    newRow["ID"] = rowOrder["ID"];
            //    newRow["NAME"] = rowOrder["DESCRIPTION"];
            //    newRow["UOM"] = "瓶";
            //    newRow["QTY"] = rowOrder["RETURN_PC"];
            //    dtReturn.Rows.Add(newRow);
            //}

            //#endregion

            //#region 插入到换货单

            //if (!string.IsNullOrEmpty(rowOrder["CHANGE_PC"].ToString()))
            //{
            //    newRow = dtChange.NewRow();
            //    newRow["ID"] = rowOrder["ID"];
            //    newRow["NAME"] = rowOrder["DESCRIPTION"];
            //    newRow["UOM"] = "瓶";
            //    newRow["QTY"] = rowOrder["CHANGE_PC"];
            //    dtChange.Rows.Add(newRow);
            //}

            //#endregion
        }

        //遍历赠品临时表
        //foreach (DataRow rowFree in dtFreeGoods.Rows)
        //{
        //    //订瓶
        //    if (!string.IsNullOrEmpty(rowFree["PC"].ToString()))
        //    {
        //        newRow = dtFree.NewRow();
        //        newRow["PROMOTION"] = rowFree["PROMOTION_ID"];
        //        newRow["ID"] = rowFree["TO_PRODUCT_ID"];
        //        newRow["NAME"] = productManager.Load(int.Parse(rowFree["TO_PRODUCT_ID"].ToString())).ProductName;
        //        newRow["UOM"] = "瓶";
        //        newRow["QTY"] = rowFree["PC"];
        //        newRow["OWN_PRODUCT"] = rowFree["PRODUCT_ID"];

        //        dtFree.Rows.Add(newRow);
        //        totalFreePC += int.Parse(rowFree["PC"].ToString());
        //    }

        //    //订箱
        //    if (!string.IsNullOrEmpty(rowFree["CASE"].ToString()))
        //    {
        //        newRow = dtFree.NewRow();
        //        newRow["PROMOTION"] = rowFree["PROMOTION_ID"];
        //        newRow["ID"] = rowFree["TO_PRODUCT_ID"];
        //        newRow["NAME"] = productManager.Load(int.Parse(rowFree["TO_PRODUCT_ID"].ToString())).ProductName;
        //        newRow["UOM"] = "箱";
        //        newRow["QTY"] = rowFree["CASE"];
        //        newRow["OWN_PRODUCT"] = rowFree["PRODUCT_ID"];
        //        dtFree.Rows.Add(newRow);
        //        totalFreeCase += int.Parse(rowFree["CASE"].ToString());
        //    }
        //}

        //没有下单不允许通过
        //if (dtOrder.Rows.Count < 1 && dtFree.Rows.Count < 1 && dtChange.Rows.Count < 1 && dtReturn.Rows.Count < 1)
        if (dtOrder.Rows.Count < 1 )
        {
            ShowMessage("不允许生成空订单!");
            return;
        }

        //更新确认的订单头界面
        lblCustomer2.Text = lblCustomer.Text;
        lblChain.Text = ddlChains.SelectedItem.Value!=""?ddlChains.SelectedItem.Text:"";
        lblSupply2.Text = lblSupply.Text;
        lblUser.Text = MyStateManager.Person_Name;
        lblOrderDateConfirm.Text = txtOrderDate.Text;
        lblRdd.Text = txtDeliveryDate.Text;
        //lblTotalPcFree.Text = totalFreePC.ToString();
        //lblTotalCaseFree.Text = totalFreeCase.ToString();
        lblTotalPcOrder.Text = totalOrderPC.ToString();
        //lblTotalCaseOrder.Text = totalOrderCase.ToString();

        //绑定订单列表
        gvOrderList.DataSource = dtOrder;
        gvOrderList.DataBind();

        //gvFreeList.DataSource = dtFree;
        //gvFreeList.DataBind();

        //gvChangeList.DataSource = dtChange;
        //gvChangeList.DataBind();

        //gvReturnList.DataSource = dtReturn;
        //gvReturnList.DataBind();

        //显示订单确认界面
        pnlOrder.Visible = false;
        pnlConfirm.Visible = true;
    }
    #endregion

    #region 选择经销商事件
    protected void ddlChains_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblSupply.Text = "";
        if (!string.IsNullOrEmpty(ddlChains.SelectedValue))
        {
            CUSTOMER_CHAINS cus_chn = new ChainManager().LoadCustomerChain(customerID, decimal.Parse(ddlChains.SelectedValue));
            try
            {
                lblSupply.Text = supplyManager.Load(cus_chn.SUPPLYID).NAME;
            }
            catch { }
        }

    }
    #endregion

    #region 绑定产品
    private void BindOrderProduct()
    {
        this.gvProductList.DataSource = ViewState["Product"] as DataTable;
        gvProductList.DataBind();
    }

    #endregion

    #region 添加产品事件

    protected void txtHidProduct_ValueChanged(object sender, EventArgs e)
    {
        string[] products = txtHidProduct.Value.Split('∮');
        PRODUCTS product;

        //更新产品表
        UpdateViewState();

        //读取临时表
        dtProducts = ViewState["Product"] as DataTable;


        //取出所有产品的ID
        foreach (string productID in products)
        {
            //判断此产品是否已经在选择产品列表中了
            if (dtProducts.Select("ID=" + productID).Length < 1)
            {
                //不在选择产品列表则添加
                product = productManager.Load(int.Parse(productID));
                USERS user = new UserManager().GetUserByID(MyStateManager.UserID);
                DataTable dtSaveProduct = ProductManager.GetProductDynamic(customerID
                                                                ,null
                                                                 , null
                                                                 ,null
                                                                 ,null
                                                                 ,null
                                                                 , MyStateManager.DomainID
                                                                 , "1"
                                                                 , "1"
                                                                ).Tables[0];

                DataRow row = dtProducts.NewRow();
                row["ID"] = productID;
                row["CODE"] = product.ProductCode;
                row["DESCRIPTION"] = product.ProductName;
                //row["ID"] = dtSaveProduct.Rows[0]["ID"].ToString();
                //row["CODE"] = dtSaveProduct.Rows[0]["productcode"].ToString();
                //row["DESCRIPTION"] = dtSaveProduct.Rows[0]["productname"].ToString();
                row["MANUFACTURE"] = dtSaveProduct.Rows[0]["ManuName"].ToString();
                row["CATEGORY"] = dtSaveProduct.Rows[0]["categoryname"].ToString();
                row["BRAND"] = dtSaveProduct.Rows[0]["brandname"].ToString();
                row["FORM"] = dtSaveProduct.Rows[0]["formname"].ToString();
                row["PIECEPRICE"] = dtSaveProduct.Rows[0]["piece_price"].ToString();
                dtProducts.Rows.Add(row);
            }
        } 

        //重新绑定选择的产品列表
        BindOrderProduct();
    }

    #endregion

    #region 删除产品
    protected void gvProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取删除产品的ID
        string productID = gvProductList.DataKeys[e.RowIndex].Value.ToString();

        //把当前产品列表更新到临时表
        UpdateViewState();

        dtProducts = ViewState["Product"] as DataTable;
        dtFreeGoods = ViewState["FreeGood"] as DataTable;

        //删除产品记录
        if (dtProducts != null)
        {
            for (int i = 0; i < dtProducts.Rows.Count; i++)
            {
                if (dtProducts.Rows[i]["ID"].ToString() == productID)
                {
                    dtProducts.Rows[i].Delete();
                    break;
                }
            }
            dtProducts.AcceptChanges();
        }

        //删除赠品记录
        //if (dtFreeGoods != null)
        //{
        //    for (int i = 0; i < dtFreeGoods.Rows.Count; i++)
        //    {
        //        if (dtFreeGoods.Rows[i]["PRODUCT_ID"].ToString() == productID)
        //            dtFreeGoods.Rows[i].Delete();
        //    }
        //    dtFreeGoods.AcceptChanges();
        //}

        //重新绑定
        ViewState["Product"] = dtProducts;
        ViewState["FreeGood"] = dtFreeGoods;
        BindOrderProduct();
    }
    #endregion

    #region 点击促销事件
    protected void gvProductList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "promotion")
        //{
        //    string freeGoods = string.Empty;
        //    dtFreeGoods = ViewState["FreeGood"] as DataTable;

        //    DataRow[] rows = dtFreeGoods.Select("PRODUCT_ID=" + e.CommandArgument.ToString());

        //    if (rows.Length > 0)
        //    {
        //        foreach (DataRow row in rows)
        //        {
        //            freeGoods += "∫" + row["PROMOTION_ID"] +":"+ row["ID"] +":" + row["TO_PRODUCT_ID"] + ":" + row["PC"] + ":" + row["CASE"];
        //        }
        //    }

        //    Response.Write(string.Format("<script>window.open('FreeGood_Select.aspx?customerID={0}&productID={1}&ValueControlId={2}&arg={3}')</script>", customerID, e.CommandArgument.ToString(), "txtHidFreeGoods", freeGoods));
        //}
    }
    #endregion

    #region 添加促销
    protected void txtHidFreeGoods_ValueChanged(object sender, EventArgs e)
    {
        /*---分割规则
         *---   原产品编号#∫促销编号:ID:赠品编号:赠瓶:赠箱 
        */

        //dtFreeGoods = ViewState["FreeGood"] as DataTable;

        ////产品编号
        //string productID = txtHidFreeGoods.Value.Split('#')[0];

        ////赠品列表
        //string[] freeGoods = txtHidFreeGoods.Value.Split('#')[1].Split('∫');

        //foreach (string freeGood in freeGoods)
        //{
        //    if (freeGood == string.Empty)
        //        continue;

        //    string[] freedGoodArray = freeGood.Split(':');

        //    DataRow[] row = dtFreeGoods.Select("ID=" + freedGoodArray[1]);

        //    if (row.Length > 0)
        //    {
        //        row[0]["PROMOTION_ID"] = freedGoodArray[0];
        //        row[0]["TO_PRODUCT_ID"] = freedGoodArray[2];
        //        row[0]["PC"] = freedGoodArray[3];
        //        row[0]["CASE"] = freedGoodArray[4];

        //        //如果返回促销没有订箱或订瓶就删除
        //        if (string.IsNullOrEmpty(freedGoodArray[3]) && string.IsNullOrEmpty(freedGoodArray[4]))
        //            row[0].Delete();

        //        dtFreeGoods.AcceptChanges();
        //    }
        //    else
        //    {
        //        if (!string.IsNullOrEmpty(freedGoodArray[3]) || !string.IsNullOrEmpty(freedGoodArray[4]))
        //        {
        //            DataRow newRow = dtFreeGoods.NewRow();
        //            newRow["PRODUCT_ID"] = productID;
        //            newRow["ID"] = freedGoodArray[1];
        //            newRow["PROMOTION_ID"] = freedGoodArray[0];
        //            newRow["TO_PRODUCT_ID"] = freedGoodArray[2];
        //            newRow["PC"] = freedGoodArray[3];
        //            newRow["CASE"] = freedGoodArray[4];
        //            dtFreeGoods.Rows.Add(newRow);
        //        }
        //    }
        //}

        //ViewState["FreeGood"] = dtFreeGoods;

    }
    #endregion

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        SaveOrder(false);
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        TextBox txtLineReceiveDate;
        CheckBox cbx;

        if (!FinancialMonthManager.IsOKtoConfirmByFinacialMonth(DateTime.Parse(txtOrderDate.Text.Trim()), MyStateManager.DomainID))
        {
            ShowMessage("订单无法确认 理由:会计期关闭");
            return;
        }

        foreach (GridViewRow rowOrder in gvOrderList.Rows)
        {
            txtLineReceiveDate = (TextBox)rowOrder.FindControl("txtRecevieDate");
            cbx = (CheckBox)rowOrder.FindControl("cbxSelected");

            if (string.IsNullOrEmpty(txtLineReceiveDate.Text) || !cbx.Checked)
                continue;

            //if (DateTime.Parse(txtLineReceiveDate.Text) > DateTime.Now)
            //{
            //    ShowMessage("订单无法确认 理由:收货日期错误");
            //    return;
            //}

            if (!FinancialMonthManager.IsOKtoConfirmByFinacialMonth(DateTime.Parse(txtLineReceiveDate.Text), MyStateManager.DomainID))
            {
                ShowMessage("订单无法确认 理由:会计期关闭");
                return;
            }
        }

        //foreach (GridViewRow rowFree in gvFreeList.Rows)
        //{
        //    txtLineReceiveDate = (TextBox)rowFree.FindControl("txtRecevieDate");
        //    cbx = (CheckBox)rowFree.FindControl("cbxSelected");

        //    if (string.IsNullOrEmpty(txtLineReceiveDate.Text) || !cbx.Checked)
        //        continue;

        //    if (DateTime.Parse(txtLineReceiveDate.Text) > DateTime.Now)
        //    {
        //        ShowMessage("订单无法确认 理由:收货日期错误");
        //        return;
        //    }

        //    if (!FinancialMonthManager.IsOKtoConfirmByFinacialMonth(DateTime.Parse(txtLineReceiveDate.Text), MyStateManager.DomainID))
        //    {
        //        ShowMessage("订单无法确认 理由:会计期关闭");
        //        return;
        //    }
        //}

        //foreach (GridViewRow rowChange in gvChangeList.Rows)
        //{
        //    txtLineReceiveDate = (TextBox)rowChange.FindControl("txtRecevieDate");
        //    cbx = (CheckBox)rowChange.FindControl("cbxSelected");

        //    if (string.IsNullOrEmpty(txtLineReceiveDate.Text) || !cbx.Checked)
        //        continue;

        //    if (DateTime.Parse(txtLineReceiveDate.Text) > DateTime.Now)
        //    {
        //        ShowMessage("订单无法确认 理由:收货日期错误");
        //        return;
        //    }

        //    if (!FinancialMonthManager.IsOKtoConfirmByFinacialMonth(DateTime.Parse(txtLineReceiveDate.Text), MyStateManager.DomainID))
        //    {
        //        ShowMessage("订单无法确认 理由:会计期关闭");
        //        return;
        //    }
        //}

        //foreach (GridViewRow rowReturn in gvReturnList.Rows)
        //{
        //    txtLineReceiveDate = (TextBox)rowReturn.FindControl("txtRecevieDate");
        //    cbx = (CheckBox)rowReturn.FindControl("cbxSelected");

        //    if (string.IsNullOrEmpty(txtLineReceiveDate.Text) && !cbx.Checked)
        //        continue;

        //    if (DateTime.Parse(txtLineReceiveDate.Text) > DateTime.Now)
        //    {
        //        ShowMessage("订单无法确认 理由:收货日期错误");
        //        return;
        //    }

        //    if (!FinancialMonthManager.IsOKtoConfirmByFinacialMonth(DateTime.Parse(txtLineReceiveDate.Text), MyStateManager.DomainID))
        //    {
        //        ShowMessage("订单无法确认 理由:会计期关闭");
        //        return;
        //    }
        //}

        SaveOrder(true);
    }

    protected void SelectALL(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk == null)
            return;
        foreach (GridViewRow row in this.gvOrderList.Rows)
        {
            CheckBox checkbox = row.Cells[gvOrderList.Columns.Count - 1].FindControl("cbxSelected") as CheckBox;
            if (checkbox != null)
                checkbox.Checked = chk.Checked;
        }
    }

    #region 生成订单

    private void SaveOrder(bool autoConfirm)
    {
        USERS user = new UserManager().GetUserByID(MyStateManager.UserID);
        TextBox txtAmount, txtLineReceiveDate;
        CheckBox cbx;
        int confirmOrder = 0;
        decimal totalQuantity = 0;
        //string orderNo = MyStateManager.UserID + DateTime.Now.ToString("yyMMddHHmmss") + "0";
        string orderNo = OrderHeaderManager.NewOrderId(customerID);

        #region 保存正常订单
        if (gvOrderList.Rows.Count > 0)
        {
            confirmOrder = 0;
            totalQuantity = 0;

            #region 插入订单明细
            foreach (GridViewRow rowOrder in gvOrderList.Rows)
            {
                txtAmount = (TextBox)rowOrder.FindControl("txtAmount");
                txtLineReceiveDate = (TextBox)rowOrder.FindControl("txtRecevieDate");
                cbx = (CheckBox)rowOrder.FindControl("cbxSelected");

                orderLines = new ORDER_LINES();
                if (Convert.ToDateTime(this.txtOrderDate.Text.Trim()).ToString("yyyyMMdd") == DateTime.Now.ToString("yyyyMMdd"))
                {
                    orderLines.ORDERNO = orderNo;
                }
                else
                {
                    orderLines.ORDERNO = orderNo;
                }

                orderLines.OrderLineStatus = (int)EOrderLineStatus.未确认;
                orderLines.PRODUCTID = int.Parse(gvOrderList.DataKeys[rowOrder.RowIndex].Value.ToString());
                orderLines.PRODUCTCODE = new ProductManager().Load(orderLines.PRODUCTID).ProductCode;
                orderLines.UOMCODE = rowOrder.Cells[1].Text == "箱" ? "CS" : "PC";
                orderLines.QUANTITYORDERED = decimal.Parse(rowOrder.Cells[7].Text);
                orderLines.DOMAINID = user.DOMAINID;
                orderLines.LAST_UPDATE_TIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                orderLines.LAST_UPDATER = MyStateManager.Name;
                orderLines.CANCELLED = "N";

                orderLines.LINEAMOUNT = decimal.Parse(rowOrder.Cells[6].Text); // *orderLines.QUANTITYORDERED;//订单单价*订单数量

                if (cbx.Checked && !string.IsNullOrEmpty(txtAmount.Text) && !string.IsNullOrEmpty(txtLineReceiveDate.Text) && autoConfirm)
                {
                    confirmOrder++;
                    orderLines.Confirm_user_code = MyStateManager.Name;
                    orderLines.Confirmed_Date =BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                    //orderLines.LINEAMOUNT = decimal.Parse(txtAmount.Text.Trim());
                    orderLines.LINEAMOUNT = decimal.Parse(rowOrder.Cells[6].Text);// *orderLines.QUANTITYORDERED;//订单单价*订单数量
                    orderLines.QUANTITYSHIPPED = orderLines.QuantityReceived = orderLines.QUANTITYORDERED;
                    orderLines.ARRIVAL_date = BusinessLogic.GetDateTimeDecimal(DateTime.Parse(txtLineReceiveDate.Text));
                    orderLines.OrderLineStatus = decimal.Parse(((int)EOrderLineStatus.已确认).ToString());
                }

                lineManager.Save(orderLines);

                //if (orderLines.UOMCODE == "CS")
                    totalQuantity += orderLines.QUANTITYORDERED;
            }
            #endregion

            #region 插入订单头
            orderHead = new ORDER_HEADERS();

            orderHead.USER_ID = MyStateManager.UserID;
            orderHead.CUSTOMER_ID = customerID;
            //如果订单不是当天的话，则为补订单
            if (Convert.ToDateTime(this.txtOrderDate.Text.Trim()).ToString("yyyyMMdd") == DateTime.Now.ToString("yyyyMMdd"))
            {
                orderHead.OrderType = (int)EOrderType.Nomal;
                orderHead.ORDER_NO = orderNo;
               
            }
            else
            {
                orderHead.OrderType = (int)EOrderType.补损订单;
                orderHead.ORDER_NO = orderNo;
               
            }
            orderHead.ID = orderHead.ORDER_NO;
            orderHead.VISIT_ID =decimal.Parse(orderHead.ORDER_NO);

            orderHead.ORDER_date = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            orderHead.REQ_DELIVERY_date = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Parse(txtDeliveryDate.Text)));
            orderHead.TOTAL_AMOUNT = orderLines.LINEAMOUNT;
            orderHead.TOTAL_QUANTITY = totalQuantity;
            orderHead.DOMAIN_ID = user.DOMAINID.ToString();
            orderHead.LAST_UPDATER = MyStateManager.Name;
            orderHead.LAST_UPDATE_TIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            orderHead.CANCELLED = "N";
            orderHead.ARRIVAL_DATE = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            if (string.IsNullOrEmpty(ddlChains.SelectedValue))
            {
                orderHead.CHAIN_ID = 0;
            }
            else
            {
                orderHead.CHAIN_ID = int.Parse(ddlChains.SelectedValue);
            }
            orderHead.ARRIVAL_DATE = orderLines.ARRIVAL_date;
            orderHead.ORG_ID = user.ORGID;
            orderHead.PERSON_ID = user.PERSON.ID;
            orderHead.ORDER_FORM = decimal.Parse(ddlOrderForm.SelectedValue);
            orderHead.EXTENDED_AMOUNT = 1;
            orderHead.DISC_RATE = 1;
            orderHead.DISC_AMOUNT = 1;
            if (confirmOrder == 0)
                orderHead.STATUS = decimal.Parse(((int)EOderStatus.未确认).ToString());
            else if (confirmOrder < gvOrderList.Rows.Count)
            {
                orderHead.STATUS = decimal.Parse(((int)EOderStatus.部分确认).ToString());
                orderHead.CONFIRMED_DATE = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
              
            }
            else if (confirmOrder == gvOrderList.Rows.Count)
            {
                orderHead.STATUS = decimal.Parse(((int)EOderStatus.已确认).ToString());
                orderHead.CONFIRMED_DATE = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            }


            headManager.Save(orderHead);

            #endregion

        }
        #endregion

        //#region 保存赠品订单
        //if (gvFreeList.Rows.Count > 0)
        //{
        //    confirmOrder = 0;
        //    totalQuantity = 0;

        //    #region 插入订单明细
        //    foreach (GridViewRow rowFree in gvFreeList.Rows)
        //    {
        //        txtAmount = (TextBox)rowFree.FindControl("txtAmount");
        //        txtLineReceiveDate = (TextBox)rowFree.FindControl("txtRecevieDate");
        //        cbx = (CheckBox)rowFree.FindControl("cbxSelected");

        //        orderLines = new ORDER_LINES();
        //        orderLines.ORDERNO = orderNo + (int)EOrderType.赠品订单;
        //        orderLines.OrderLineStatus = (int)EOrderLineStatus.未确认;
        //        orderLines.PRODUCTID = int.Parse(gvFreeList.DataKeys[rowFree.RowIndex].Values["ID"].ToString());
        //        orderLines.PRODUCTCODE = new ProductManager().Load(orderLines.PRODUCTID).ProductCode;
        //        orderLines.UOMCODE = rowFree.Cells[1].Text == "箱" ? "CS" : "PC";
        //        orderLines.QUANTITYORDERED = decimal.Parse(rowFree.Cells[2].Text);
        //        orderLines.OWNPROID = int.Parse(gvFreeList.DataKeys[rowFree.RowIndex].Values["OWN_PRODUCT"].ToString());
        //        orderLines.PROMOTIONID = decimal.Parse(gvFreeList.DataKeys[rowFree.RowIndex].Values["PROMOTION"].ToString());
        //        orderLines.DOMAINID = user.DOMAINID;
        //        orderLines.LAST_UPDATE_TIME = DateTime.Now;
        //        orderLines.LAST_UPDATE = MyStateManager.Name;
        //        orderLines.CANCELLED = "N";

        //        if (cbx.Checked && !string.IsNullOrEmpty(txtAmount.Text) && !string.IsNullOrEmpty(txtLineReceiveDate.Text) && autoConfirm)
        //        {
        //            confirmOrder++;
        //            orderLines.Confirm_user_code = MyStateManager.Name;
        //            orderLines.Confirmed_Date = DateTime.Now;
        //            orderLines.LINEAMOUNT = decimal.Parse(txtAmount.Text.Trim());
        //            orderLines.QUANTITYSHIPPED = orderLines.QuantityReceived = orderLines.QUANTITYORDERED;
        //            orderLines.ARRIVAL_date = DateTime.Parse(txtLineReceiveDate.Text);
        //            orderLines.OrderLineStatus = decimal.Parse(((int)EOrderLineStatus.已确认).ToString());
        //        }

        //        lineManager.Save(orderLines);

        //        if (orderLines.UOMCODE == "CS")
        //            totalQuantity += orderLines.QUANTITYORDERED;
        //    }
        //    #endregion

        //    #region 插入订单头
        //    orderHead = new ORDER_HEADERS();
        //    orderHead.USERID = decimal.Parse(ddlUsers.SelectedValue);
        //    orderHead.CUSTOMERID = customerID;
        //    orderHead.OrderType = (int)EOrderType.赠品订单;
        //    orderHead.ORDERNO = orderNo + (int)EOrderType.赠品订单;
        //    orderHead.ORDERdate = DateTime.Now;
        //    orderHead.REQDELIVERYdate = DateTime.Parse(txtDeliveryDate.Text);
        //    orderHead.TOTALAMOUNT = 0;
        //    orderHead.TOTALQUANTITY = totalQuantity;
        //    orderHead.EXTENDEDAMOUNT = 0;
        //    orderHead.STATUS = (int)EOderStatus.未确认;
        //    orderHead.DOMAINID = user.DOMAINID;
        //    orderHead.TRUCKCODE = "1";
        //    orderHead.DISCRATE = 0;
        //    orderHead.DISCAMOUNT = 0;
        //    orderHead.LAST_UPDATE = MyStateManager.Name;
        //    orderHead.LAST_UPDATE_TIME = DateTime.Now;
        //    orderHead.CANCELLED = "N";
        //    orderHead.CHAINID = int.Parse(ddlChains.SelectedValue);
        //    orderHead.ORGID = user.ORGID;
        //    orderHead.PERSONID = user.PERSON.ID;
        //    orderHead.ORDERFORM = decimal.Parse(ddlOrderForm.SelectedValue);

        //    if (confirmOrder == 0)
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.未确认).ToString());
        //    else if (confirmOrder < gvFreeList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.部分确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;
        //    }
        //    else if (confirmOrder == gvFreeList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.已确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;
        //    }

        //    headManager.Save(orderHead);

        //    #endregion
        //}
        //#endregion

        //#region 保存换货订单
        //if (gvChangeList.Rows.Count > 0)
        //{
        //    confirmOrder = 0;
        //    totalQuantity = 0;

        //    #region 插入订单明细
        //    foreach (GridViewRow rowChange in gvChangeList.Rows)
        //    {
        //        txtAmount = (TextBox)rowChange.FindControl("txtAmount");
        //        txtLineReceiveDate = (TextBox)rowChange.FindControl("txtRecevieDate");
        //        cbx = (CheckBox)rowChange.FindControl("cbxSelected");

        //        orderLines = new ORDER_LINES();
        //        orderLines.ORDERNO = orderNo + (int)EOrderType.换货订单;
        //        orderLines.OrderLineStatus = (int)EOrderLineStatus.未确认;
        //        orderLines.PRODUCTID = int.Parse(gvChangeList.DataKeys[rowChange.RowIndex].Values["ID"].ToString());
        //        orderLines.PRODUCTCODE = new ProductManager().Load(orderLines.PRODUCTID).ProductCode;
        //        orderLines.UOMCODE = "PC";
        //        orderLines.QUANTITYORDERED = decimal.Parse(rowChange.Cells[2].Text);
        //        orderLines.DOMAINID = user.DOMAINID;
        //        orderLines.LAST_UPDATE_TIME = DateTime.Now;
        //        orderLines.LAST_UPDATE = MyStateManager.Name;
        //        orderLines.CANCELLED = "N";

        //        if (cbx.Checked && !string.IsNullOrEmpty(txtAmount.Text) && !string.IsNullOrEmpty(txtLineReceiveDate.Text) && autoConfirm)
        //        {
        //            confirmOrder++;
        //            orderLines.Confirm_user_code = MyStateManager.Name;
        //            orderLines.Confirmed_Date = DateTime.Now;
        //            orderLines.LINEAMOUNT = decimal.Parse(txtAmount.Text.Trim());
        //            orderLines.QUANTITYSHIPPED = orderLines.QuantityReceived = orderLines.QUANTITYORDERED;
        //            orderLines.ARRIVAL_date = DateTime.Parse(txtLineReceiveDate.Text);
        //            orderLines.OrderLineStatus = decimal.Parse(((int)EOrderLineStatus.已确认).ToString());
        //        }

        //        lineManager.Save(orderLines);

        //        if (orderLines.UOMCODE == "CS")
        //            totalQuantity += orderLines.QUANTITYORDERED;
        //    }
        //    #endregion

        //    #region 插入订单头
        //    orderHead = new ORDER_HEADERS();
        //    orderHead.USERID = decimal.Parse(ddlUsers.SelectedValue);
        //    orderHead.CUSTOMERID = customerID;
        //    orderHead.OrderType = (int)EOrderType.换货订单;
        //    orderHead.ORDERNO = orderNo + (int)EOrderType.换货订单;
        //    orderHead.ORDERdate = DateTime.Now;
        //    orderHead.REQDELIVERYdate = DateTime.Parse(txtDeliveryDate.Text);
        //    orderHead.TOTALAMOUNT = 0;
        //    orderHead.TOTALQUANTITY = totalQuantity;
        //    orderHead.EXTENDEDAMOUNT = 0;
        //    orderHead.STATUS = (int)EOderStatus.未确认;
        //    orderHead.DOMAINID = user.DOMAINID;
        //    orderHead.TRUCKCODE = "1";
        //    orderHead.DISCRATE = 0;
        //    orderHead.DISCAMOUNT = 0;
        //    orderHead.LAST_UPDATE = MyStateManager.Name;
        //    orderHead.LAST_UPDATE_TIME = DateTime.Now;
        //    orderHead.CANCELLED = "N";
        //    orderHead.CHAINID = int.Parse(ddlChains.SelectedValue);
        //    orderHead.ORGID = user.ORGID;
        //    orderHead.PERSONID = user.PERSON.ID;
        //    orderHead.ORDERFORM = decimal.Parse(ddlOrderForm.SelectedValue);

        //    if (confirmOrder == 0)
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.未确认).ToString());
        //    else if (confirmOrder < gvChangeList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.部分确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;

        //    }
        //    else if (confirmOrder == gvChangeList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.已确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;

        //    }

        //    headManager.Save(orderHead);

        //    #endregion

        //}
        //#endregion

        //#region 保存退货订单
        //if (gvReturnList.Rows.Count > 0)
        //{
        //    confirmOrder = 0;
        //    totalQuantity = 0;

        //    #region 插入订单明细
        //    foreach (GridViewRow rowReturn in gvReturnList.Rows)
        //    {
        //        txtAmount = (TextBox)rowReturn.FindControl("txtAmount");
        //        txtLineReceiveDate = (TextBox)rowReturn.FindControl("txtRecevieDate");
        //        cbx = (CheckBox)rowReturn.FindControl("cbxSelected");

        //        orderLines = new ORDER_LINES();
        //        orderLines.ORDERNO = orderNo + (int)EOrderType.退货订单;
        //        orderLines.OrderLineStatus = (int)EOrderLineStatus.未确认;
        //        orderLines.PRODUCTID = int.Parse(gvReturnList.DataKeys[rowReturn.RowIndex].Values["ID"].ToString());
        //        orderLines.PRODUCTCODE = new ProductManager().Load(orderLines.PRODUCTID).ProductCode;
        //        orderLines.UOMCODE = "PC";
        //        orderLines.QUANTITYORDERED = decimal.Parse(rowReturn.Cells[2].Text);
        //        orderLines.DOMAINID = user.DOMAINID;
        //        orderLines.LAST_UPDATE_TIME = DateTime.Now;
        //        orderLines.LAST_UPDATE = MyStateManager.Name;
        //        orderLines.CANCELLED = "N";

        //        if (cbx.Checked && !string.IsNullOrEmpty(txtAmount.Text) && !string.IsNullOrEmpty(txtLineReceiveDate.Text) && autoConfirm)
        //        {
        //            confirmOrder++;
        //            orderLines.Confirm_user_code = MyStateManager.Name;
        //            orderLines.Confirmed_Date = DateTime.Now;
        //            orderLines.LINEAMOUNT = decimal.Parse(txtAmount.Text.Trim());
        //            orderLines.QUANTITYSHIPPED = orderLines.QuantityReceived = orderLines.QUANTITYORDERED;
        //            orderLines.ARRIVAL_date = DateTime.Parse(txtLineReceiveDate.Text);
        //            orderLines.OrderLineStatus = decimal.Parse(((int)EOrderLineStatus.已确认).ToString());
        //        }

        //        lineManager.Save(orderLines);

        //        if (orderLines.UOMCODE == "CS")
        //            totalQuantity += orderLines.QUANTITYORDERED;
        //    }
        //    #endregion

        //    #region 插入订单头
        //    orderHead = new ORDER_HEADERS();
        //    orderHead.USERID = decimal.Parse(ddlUsers.SelectedValue);
        //    orderHead.CUSTOMERID = customerID;
        //    orderHead.OrderType = (int)EOrderType.退货订单;
        //    orderHead.ORDERNO = orderNo + (int)EOrderType.退货订单;
        //    orderHead.ORDERdate = DateTime.Now;
        //    orderHead.REQDELIVERYdate = DateTime.Parse(txtDeliveryDate.Text);
        //    orderHead.TOTALAMOUNT = 0;
        //    orderHead.TOTALQUANTITY = totalQuantity;
        //    orderHead.EXTENDEDAMOUNT = 0;
        //    orderHead.STATUS = (int)EOderStatus.未确认;
        //    orderHead.DOMAINID = user.DOMAINID;
        //    orderHead.TRUCKCODE = "1";
        //    orderHead.DISCRATE = 0;
        //    orderHead.DISCAMOUNT = 0;
        //    orderHead.LAST_UPDATE = MyStateManager.Name;
        //    orderHead.LAST_UPDATE_TIME = DateTime.Now;
        //    orderHead.CANCELLED = "N";
        //    orderHead.CHAINID = int.Parse(ddlChains.SelectedValue);
        //    orderHead.ORGID = user.ORGID;
        //    orderHead.PERSONID = user.PERSON.ID;
        //    orderHead.ORDERFORM = decimal.Parse(ddlOrderForm.SelectedValue);

        //    if (confirmOrder == 0)
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.未确认).ToString());
        //    else if (confirmOrder < gvReturnList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.部分确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;

        //    }
        //    else if (confirmOrder == gvReturnList.Rows.Count)
        //    {
        //        orderHead.STATUS = decimal.Parse(((int)EOderStatus.已确认).ToString());
        //        orderHead.Confirmusercode = MyStateManager.Name;
        //        orderHead.ConfirmedDate = DateTime.Now;

        //    }

        //    headManager.Save(orderHead);

        //    #endregion

        //}
        //#endregion

        ShowMessage("订单生成成功,将跳转到选择门店页面!", "Customer_Select.aspx");
    }
    #endregion

    #region 判断产品是否有促销
    protected void gvProductList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //获取该行的产品ID
            //decimal productID = decimal.Parse(DataBinder.Eval(e.Row.DataItem, "ID").ToString());

            ////找到促销按钮
            //Button btn = (Button)e.Row.FindControl("btnPromotion");

            //判断此客户的该产品是否存在促销
            //DataTable dt = new OrderPromotionManager().GetFreeGoodByCustomerAndProduct(customerID, productID);

            //没促销，按钮置为无效
            //if (dt.Rows.Count < 1)
            //    btn.Enabled = false;
        }
    }
    #endregion

    #region 选择订单来源
    protected void ddlOrderForm_SelectedIndexChanged(object sender, EventArgs e)
    {
        //if (ddlOrderForm.SelectedItem.Text.IndexOf("补录") > -1)
        //{
            txtDeliveryDate.Text = DateTime.Parse(txtOrderDate.Text).AddDays(1).ToString("yyyy/MM/dd");

            //绑定业务代表
            //ddlUsers.Items.Clear();
            //ddlUsers.DataSource = routeManager.GetUsersByCustomer(customerID);
            //ddlUsers.DataTextField = "NAME";
            //ddlUsers.DataValueField = "ID";
            //ddlUsers.DataBind();

        //}
        //else
        //{
        //    txtDeliveryDate.Text = DateTime.Parse(txtOrderDate.Text).ToString("yyyy-MM-dd");

        //    //绑定业务代表
        //    ddlUsers.Items.Clear();
        //    ddlUsers.DataSource = routeManager.GetUsersForOrder(customerID);
        //    ddlUsers.DataTextField = "NAME";
        //    ddlUsers.DataValueField = "ID";
        //    ddlUsers.DataBind();
        //    ddlUsers.Items.Insert(0, new ListItem("--请选择", ""));
        //}
    }
    #endregion

    protected void gvOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txt = (TextBox)e.Row.FindControl("txtRecevieDate");
            txt.Text = txtDeliveryDate.Text;
        }
    }
    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "", string.Format("openWindow('Product_Select.aspx?ValueControlId=" + txtHidProduct.ClientID + "&repID={0}&customerID={1}','添加产品',500,500)", MyStateManager.UserID, customerID), true);
        //ScriptManager.RegisterStartupScript(this, GetType(), "", string.Format("window.showModelessDialog('Product_Select.aspx?ValueControlId=" + txtHidProduct.UniqueID + "&repID={0}&customerID={1}','','dialogWidth=800px;dialogHeight=700px')", ddlUsers.SelectedValue, customerID), true);
    }
}
